package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.api.UUser;
import com.ruoyi.system.service.IUUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.api.UUserAccountDetail;
import com.ruoyi.system.service.IUUserAccountDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账户详情Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/api/uUserAccountDetail")
public class UUserAccountDetailController extends BaseController
{
    @Autowired
    private IUUserAccountDetailService uUserAccountDetailService;

    @Autowired
    private IUUserService userService;

    /**
     * 查询账户详情列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(UUserAccountDetail uUserAccountDetail)
    {
        if (!StringUtils.isEmpty(uUserAccountDetail.getUserPhone())){
            UUser user = new UUser();
            if (uUserAccountDetail.getUserPhone().contains("@")){
                user.setEmail(uUserAccountDetail.getUserPhone());
            }else {
                user.setMobile(uUserAccountDetail.getUserPhone());
            }

            List<UUser> uUsers = userService.selectUUserList(user);
            if (!uUsers.isEmpty()){
                uUserAccountDetail.setUserId(uUsers.get(0).getId());
            }else {
                uUserAccountDetail.setUserId(-1L);
            }
        }
        startPage();
        List<UUserAccountDetail> list = uUserAccountDetailService.selectUUserAccountDetailList(uUserAccountDetail);

        if (!list.isEmpty()){
            for (UUserAccountDetail u:list) {
                UUser user = userService.selectUUserById(u.getUserId());
                if (user != null){
                    if (user.getMobile() != null){
                        u.setUserPhone(user.getMobile());
                    }else {
                        u.setUserPhone(user.getEmail());
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出账户详情列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:export')")
    @Log(title = "账户详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UUserAccountDetail uUserAccountDetail)
    {
        List<UUserAccountDetail> list = uUserAccountDetailService.selectUUserAccountDetailList(uUserAccountDetail);
        ExcelUtil<UUserAccountDetail> util = new ExcelUtil<UUserAccountDetail>(UUserAccountDetail.class);
        util.exportExcel(response, list, "账户详情数据");
    }

    /**
     * 获取账户详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(uUserAccountDetailService.selectUUserAccountDetailById(id));
    }

    /**
     * 新增账户详情
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:add')")
    @Log(title = "账户详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UUserAccountDetail uUserAccountDetail)
    {
        return toAjax(uUserAccountDetailService.insertUUserAccountDetail(uUserAccountDetail));
    }

    /**
     * 修改账户详情
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:edit')")
    @Log(title = "账户详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UUserAccountDetail uUserAccountDetail)
    {
        return toAjax(uUserAccountDetailService.updateUUserAccountDetail(uUserAccountDetail));
    }

    /**
     * 删除账户详情
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccountDetail:remove')")
    @Log(title = "账户详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uUserAccountDetailService.deleteUUserAccountDetailByIds(ids));
    }
}
